#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

int minimumSum(vector<int>& nums) {
    int n = nums.size();
    vector<int> suf(n);
    suf[n - 1] = nums[n - 1];
    for (int i = n - 2; i > 1; i--)
        suf[i] = min(suf[i + 1], nums[i]);

    int res = INT_MAX;
    int pre = nums[0];
    for (int i = 1; i < n - 1; i++)
    {
        if (pre < nums[i] && nums[i] > suf[i + 1])
            res = min(res, pre + nums[i] + suf[i + 1]);
        pre = min(pre, nums[i]);
    }
    return res == INT_MAX ? -1 : res;
}